<?php

namespace plugin\ycMassage\app\api\controller;

use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\PluginArticles;
use plugin\ycMassage\app\model\PluginArticlesCate;
use plugin\ycMassage\app\model\PluginTags;
use support\Request;

class ArticleController extends BaseController
{
    protected $noNeedLogin = ["getSelect", "getContent", "getContentByAlias"];
    public function getSelect(Request $request)
    {
        $alias = $request->get('alias');
        $saas_appid = $request->appid;
        if ($request->saas_appid) {
            $saas_appid = $request->saas_appid;
        }
        $cid = 0;
        switch ($alias) {
            case 'notice':
                $cid = PluginArticlesCate::where(['saas_appid' => $saas_appid, 'alias' => 'notice'])->value('id');
                break;
        }
        if (!$cid) {
            return $this->fail('分类不存在');
        }
        $data = PluginArticles::where(['saas_appid' => $saas_appid, 'cid' => $cid, 'status' => '20'])->order('id desc')->field('id,saas_appid,title,desc,create_at,is_alert,view')->select();
        return $this->successRes($data);
    }
    public function getContent(Request $request)
    {
        $id = $request->get('id');
        $saas_appid = $request->appid;
        if ($request->saas_appid) {
            $saas_appid = $request->saas_appid;
        }
        $data = PluginArticles::where(['saas_appid' => $saas_appid, 'id' => $id, 'status' => '20'])->find();
        if ($data) {
            return $this->successRes($data);
        }
        return $this->fail('数据不存在');
    }
    public function getContentByAlias(Request $request)
    {
        $alias = $request->get('alias');
        $saas_appid = $request->appid;
        if ($request->saas_appid) {
            $saas_appid = $request->saas_appid;
        }
        $data = PluginTags::where(['saas_appid' => $saas_appid, 'name' => $alias, 'status' => '20'])->find();
        if ($data) {
            return $this->successRes($data);
        }
        return $this->fail('数据不存在');
    }
}
